﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using System.Windows.Threading;
using Central_Control.Key;
using Central_Control.展厅;
namespace Central_Control
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private DispatcherTimer timer; //计时器
        Central_Control.Key.Warning warning;   //处理激活码类
        MainWindow mainWindow;
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // 初始化定时器
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromDays(1); // 设置检查间隔，例如每30分钟检查一次
            timer.Tick += Timer_Tick;

            // 启动定时器
            //timer.Start();

            //ObservableCollection<GalleryButtonInfo> buttons;
            //try
            //{
            //    // 从JSON文件加载按钮信息
            //    string json = File.ReadAllText("buttonInfo.json");
            //    buttons = JsonConvert.DeserializeObject<ObservableCollection<GalleryButtonInfo>>(json);
            //}
            //catch (FileNotFoundException)
            //{
            //    // 如果文件不存在，创建一个空的按钮集合
            //    buttons = new ObservableCollection<GalleryButtonInfo>();
            //}
            //catch (JsonException)
            //{
            //    // 如果JSON解析失败，创建一个空的按钮集合
            //    buttons = new ObservableCollection<GalleryButtonInfo>();
            //}

            Init();
            //AddPC addPC = new AddPC();
            //addPC.Show();

            //Gallery gallery = new Gallery();
            //gallery.Show();

            //Window1 window1 = new Window1();
            //window1.Show();

            //shutDown shutDown = new shutDown();
            //shutDown.Show();
        }

        private void Timer_Tick(object sender, EventArgs e)  //定时检查激活码是否时效
        {
            // 在这里执行定期检查许可证状态的逻辑
            warning.CheckData();
        }


        void Init()
        {
            mainWindow = new MainWindow();
            mainWindow.Show();
            warning = new Key.Warning();
            string localAppDataDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Central Control");
            if (!Directory.Exists(localAppDataDirectory))
            {
                Directory.CreateDirectory(localAppDataDirectory);
                warning.CreatKeyJson();
                mainWindow.CreatGalleryInfoJson();
            }
            warning.CheckData();
        }
        //private void CheckActivationKey()   //实例化激活码类并检查一次
        //{
        //    warning = new Key.Warning();
        //    string localAppDataDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Central Control");

        //    if (!Directory.Exists(localAppDataDirectory))
        //    {
        //        Directory.CreateDirectory(localAppDataDirectory);
        //        warning.CreatKeyJson();

        //    }

        //    warning.CheckData();
        //}
    }

}
